package cn.mgm.advanced.pool;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

public class DruidTest {

    /**
     * druid硬编码
     * 1. 创建 druidDataSource连接池对象
     * 2. 设置连接池的信息
     * 3. 通过连接池获取连接对象
     * 4. 回收连接池[不是释放连接,而是把连接归还给连接池,供其他线程使用]
     */
    @Test
    public void hardCodeTest() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://139.224.197.217:3336/guigu-auth");
        druidDataSource.setUrl("root");
        druidDataSource.setPassword("kaa3akv8");

        druidDataSource.setInitialSize(10);
        druidDataSource.setMaxActive(20);

        DruidPooledConnection connection = druidDataSource.getConnection();

        connection.close();
    }


    /**
     * 创建 properties 集合,用于存储配置文件 key 和 value 的值
     * 读取外部配置文件,获取输入流,加载到 properties 集合中
     */
    @Test
    public void softCodeTest() throws Exception {
        Properties properties = new Properties();
        InputStream resourceAsStream = DruidTest.class.getClassLoader().getResourceAsStream("db.properties");
        properties.load(resourceAsStream);
        System.out.println(properties);

        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        System.out.println(dataSource);
    }
}


